/**
 * Copyright 2011 Joe Choi, All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tec.data.persistence;

import java.util.HashMap;
import java.util.Map;

import tao.data.persistence.Field;
import tao.data.persistence.Record;
import tao.data.structure.List;

/**
 * @author Joe Choi
 */
public class ConcreteRecord implements Record {
	public ConcreteRecord(List<Field> heading, List<Object> data) {
		if(heading.size() != data.size()) throw new java.lang.IllegalArgumentException();
		this.heading = heading;
		this.data = data;
		this.headingMap = new HashMap<Field, Object>();
		this.fieldMap = new HashMap<String, Object>();
		
		for(int i=0; i<heading.size(); i++) {
			headingMap.put(heading.get(i), data.get(i));
			fieldMap.put(heading.get(i).getName().toUpperCase(), data.get(i));
		}
	}
	
	@Override
	public Object getValue(int fieldIndex) {
		return(data.get(fieldIndex));
	}

	@Override
	public Object getValue(String name) {
		return(fieldMap.get(name.toUpperCase()));
	}

	@Override
	public Object getValue(Field f) {
		return(headingMap.get(f));
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null || (!(obj instanceof ConcreteRecord))) return(false);
		ConcreteRecord other = (ConcreteRecord) obj;
		return(this.heading.equals(other.heading) && this.data.equals(other.data));
	}
	
	@Override
	public int hashCode() {
		return(heading.hashCode() + data.hashCode());
	}
	
	@Override
	public String toString() {
		return(heading.toString() + ":" + data.toString());
	}
	
	private List<Field> heading;
	private Map<Field, Object> headingMap;
	private Map<String, Object> fieldMap;
	private List<Object> data;
}